package online.hxstart.code.concurrent.chapter05;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import online.hxstart.code.concurrent.chapter05.EventQueue.Event;

/**
 * @author huxing
 * @date 19-4-24 星期三 下午12:31
 * @describe
 */
public class EventClient {

  public static void main(String[] args) {
    final EventQueue eventQueue = new EventQueue();
    new Thread(
            () -> {
              for (; ; ) {
                eventQueue.offer(new Event());
              }
            },
            "Producer")
        .start();
    IntStream.range(0, 38)
        .boxed()
        .map(
            item ->
                new Thread(
                    () -> {
                      for (; ; ) {
                        eventQueue.take();
                        try {
                          TimeUnit.MILLISECONDS.sleep(10);
                        } catch (InterruptedException e) {
                          e.printStackTrace();
                        }
                      }
                    },
                    "Consumer" + item))
        .forEach(Thread::start);
  }
}
